﻿<html>

<head>
    <title></title>
    <meta charset="utf-8" />
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Expires" content="0">
    <script src="index.js"></script>
    <script src="jquery.min.js"></script>
    <script src="jsencrypt.min.js"></script>
    <script src="base64.js"></script>
</head>

<body>
    <div>
        <div id='video' style="width:100% !important;height:100% !important;"></div>
    </div>
    <script>
        var player = undefined
        function initVideo(id, url) {
            var container = document.getElementById(id)
            player = new window.Jessibuca({ container: container, videoBuffer: 0.2, isResize: false, forceNoOffscreen: true, isNotMute: false, timeout: 10 })
            player.on('load', function () {
                player.play(url)
            })
            player.on('error', function () {
                alert("播放错误")
            })
            player.on('timeout', function () {
                alert("播放超时")
            })
        }

        var ip = getUrlParameters().ip;   // 设备局域网IP
        var port = 80;              // 设备网页端口
        var proto = "http";         // 协议，http 或 https
        var username = "admin";     // 设备网页登录用户名
        var password = "admin";     // 设备网页登录密码

        var url = proto + "://" + ip + ":" + port + "/request.php"
        var data = {
            type: 'get_live_stream_type',
            module: 'BUS_WEB_REQUEST'
        }
        $.ajax({
            type: "POST",
            url: url,
            data: JSON.stringify(data),
            success: function (ajaxdata) {
                if (ajaxdata.state !== 200) {
                    alert("该设备不支持FLV")
                } else {
                    var video_port = ajaxdata.body.port
                    var pubkey = Base64.decode(ajaxdata.body.pubkey)
                    let encryptStr = new JSEncrypt()
                    encryptStr.setPublicKey(pubkey)
                    var str = username + ':' + password
                    var token = encodeURIComponent(encryptStr.encrypt(str.toString()))

                    var video_url = "ws://" + ip + ":" + video_port + "/ws.flv?token=" + token;
                    initVideo("video", video_url)
                }
            },
            error: function (ajaxdata) {
                alert(ajaxdata);
            }
        });
        function getUrlParameters() {
            var urlParams = {};
            var match, regex = /([^&=]+)=([^&]*)/g;
            var searchString = window.location.search.substring(1);
            while ((match = regex.exec(searchString))) {
                var key = decodeURIComponent(match[1]);
                var value = decodeURIComponent(match[2]);
                urlParams[key] = value;
            }
            return urlParams;
        }
    </script>
</body>

</html>